package com.easymusic;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener; 

public class EasyMusicSelectDirectory implements OnItemClickListener, OnItemLongClickListener{
	private ListView m_list;
	private Context m_context;
	private AlertDialog.Builder m_builder;
	private String m_strCurrentPath;
	private Dialog m_dlg;
	
	public EasyMusicSelectDirectory(Context context, String strCurrentPath){
		m_builder = new AlertDialog.Builder(context);
		m_context = context;
		m_list = new ListView(m_context);
		m_strCurrentPath = strCurrentPath;
		EasyMusicAdapter adapter = new EasyMusicAdapter(context, m_strCurrentPath);
		m_list.setAdapter(adapter);
		m_list.setOnItemClickListener(this);
		m_list.setOnItemLongClickListener(this);
		m_builder.setView(m_list);
	}
	
	public Dialog showDialog(){
		m_dlg = m_builder.create();
		m_dlg.setTitle("long click to select a folder...");
		return m_dlg;
	}
	
	public void setCurrentPath(String strCurrentPath){
		m_strCurrentPath = strCurrentPath;
	}
	
	public class EasyMusicAdapter extends BaseAdapter{
		ArrayList<View> m_views = new ArrayList<View>();
		
		public EasyMusicAdapter(Context context, String strPath){
			File file = new File(strPath);
			FileFilter filter = new FileFilter(){

				@Override
				public boolean accept(File arg0) {
					return arg0.isDirectory();
				}
			};
			
			File[] dirs = file.listFiles(filter);
			
			if (strPath.compareTo("/") != 0){
				m_views.add(createView(context, ".."));
			}
			
			if (null == dirs){
				return;
			}
			
			for (File d: dirs){				
				m_views.add(createView(context, d.getName()));
			}
		} 
		
		private View createView(Context context, String strText){
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			View view = inflater.inflate(R.layout.select_folder, null);
			
			ImageView imageView = (ImageView)view.findViewById(R.id.folderimage);
			imageView.setImageResource(R.drawable.folder);
			
			TextView text = (TextView)view.findViewById(R.id.foldertext);
			text.setText(strText);
			
			return view;
		}
		
		@Override
		public int getCount() {
			return m_views.size();
		}

		@Override
		public Object getItem(int arg0) {
			return m_views.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			return m_views.get(arg0);
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
		// TODO Auto-generated method stub
		TextView text = (TextView)view.findViewById(R.id.foldertext);
		setNextPath((String)text.getText());
		EasyMusicAdapter adapter = new EasyMusicAdapter(m_context, m_strCurrentPath);
		m_list.setAdapter(adapter);
	}
	
	private void setNextPath(String strNextPathName){
		if (strNextPathName.compareTo("..") == 0){
			int iIndex = m_strCurrentPath.lastIndexOf('/');
			if (iIndex == m_strCurrentPath.indexOf('/')){
				m_strCurrentPath = "/";
			}else{
				m_strCurrentPath = m_strCurrentPath.substring(0, iIndex);
			}
		}else{
			if (m_strCurrentPath.charAt(m_strCurrentPath.length() - 1) == '/'){
				m_strCurrentPath = m_strCurrentPath + strNextPathName;
			}else{
				m_strCurrentPath = m_strCurrentPath + "/" + strNextPathName;
			}
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View view, int arg2,
			long arg3) {
		TextView text = (TextView)view.findViewById(R.id.foldertext);
		setNextPath((String)text.getText());
		EasyMusicAdapter adapter = new EasyMusicAdapter(m_context, m_strCurrentPath);
		m_list.setAdapter(adapter);
		m_dlg.dismiss();// TODO Auto-generated method stub
		return false;
	}
	
	public String getCurrentPath(){
		return m_strCurrentPath;
	}
}





































